﻿using System;
using System.Collections.Generic;
using System.Windows.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using carXapp2;
using System.Collections.ObjectModel;
using System.ComponentModel;
using carXapp2.classes;
using AmCharts.Windows.QuickCharts;
using System.Threading;

namespace carXapp2.FuncPages
{
    public partial class FuelInfo : PhoneApplicationPage
    {
        private string carID;
        private CarStats stats;
      

        public FuelInfo()
        {
            
            InitializeComponent();
            this.DataContext = App.ViewModel;
            //loading screen code
            
        }

       

        private ObservableCollection<fuelInfo> _fInfo;

        public ObservableCollection<fuelInfo> CurrentFInfo
    {
        get {return _fInfo;}
            set
            {
                _fInfo=value;
                NotifyPropertyChanged("FInfo");
            }

    }

        private void LoadData(int id)
        {
           var fuelInfoWithCarID = from fuelInfo info in App.ViewModel.carDataDb.fuelInfo
                                   where info.CarID==id
                                   orderby info.Date descending
                                   select info;
           CurrentFInfo = new ObservableCollection<fuelInfo>(fuelInfoWithCarID);
           //CurrentFInfo.OrderByDescending(n => n.FuelID);
           App.ViewModel.Fuel = CurrentFInfo;
           App.ViewModel.SaveChangesToDB(); //fix to save data on immediate exit 
           var fuelInfoforStats = from fuelInfo info in App.ViewModel.carDataDb.fuelInfo
                                   where info.CarID == id
                                   orderby info.Date ascending
                                   select info;
           CurrentFInfo = new ObservableCollection<fuelInfo>(fuelInfoforStats);
           stats = new CarStats(CurrentFInfo);
           double[] mpg = stats.GetMPG(Settings.MPGMethod);
           int count = App.ViewModel.Fuel.Count;
           foreach (fuelInfo mpgRecUpdate in App.ViewModel.Fuel)
           {
               count--;
               mpgRecUpdate.Mileage = mpg[count];
               
           }
           CreateChart(0);
           chart1.DataContext = this;

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("carID", out carID);

            LoadData(int.Parse(carID));
            base.OnNavigatedTo(e);
        }

        
       

        private void menuADD_Click(object sender, EventArgs e)
        {
            App.ViewModel.SingleFuelRecord = new fuelInfo();
            App.ViewModel.SingleFuelRecord.Date = DateTime.Now;
            App.ViewModel.SingleFuelRecord.CarID = int.Parse(carID);
            NavigationService.Navigate(new Uri("/MiscPages/AddEditFuel.xaml?carID="+carID,UriKind.Relative));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        private void menuEdit_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                var selectedRecord = listBox1.SelectedItem as fuelInfo;
                var FuelID = selectedRecord.FuelID;
                App.ViewModel.SingleFuelRecord = selectedRecord;
                NavigationService.Navigate(new Uri("/MiscPages/AddEditFuel.xaml?carID=" + carID + "&FuelID=" + FuelID, UriKind.Relative));
            }
            else
            {
                MessageBox.Show("please select a record");
            }
        }

        private void menuDel_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                var selectedRecord = listBox1.SelectedItem as fuelInfo;
                App.ViewModel.DeleteFuel(selectedRecord);
            }
            else
            {
                MessageBox.Show("please select a record");
            }
        }

        private void CreateChart(int selection)
        {
            ChartDataSet.Clear();
            string[] monthList = new string[13] { "0", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            int count = 0;
            switch (selection)
            {
                case 0:
                    float[] dataList;
                    
                    dataList = stats.GetMPGListbyMonth(Settings.MPGMethod);
                   
                    foreach (float item in dataList)
                    {
                        ChartData data = new ChartData() {val1=monthList[count], val2=item };
                        ChartDataSet.Add(data);
                        count++;
                    }
                    //graph1.Title = "Fuel economy trend";
                    break;
                case 1:
                    dataList = null;
                    dataList = stats.GetTotalFuelCostByMonth();
                    
                    foreach (float item in dataList)
                    {
                        ChartData data = new ChartData() {val1=monthList[count], val2 = item };
                        ChartDataSet.Add(data);
                        count++;
                    }
                    //graph1.Title = "Monthly cost trend";
                    break;
            }

        }

        private void ChartSelectionChanged(object sender, MouseEventArgs e)
        {
            if (listpicker1.SelectedIndex != -1)
            {
                CreateChart(listpicker1.SelectedIndex);
            }
        }

        private ObservableCollection<ChartData> _ChartData = new ObservableCollection<ChartData>();
        
        public ObservableCollection<ChartData> ChartDataSet 
        { get { return _ChartData; }
            set
            {
                _ChartData = value;
            }
        }
        public class ChartData
        {
            public string val1 { get; set; }
            public double val2 { get; set; }
            
        }

    }
}